/*
 * $Id: QSO.m 84 2009-07-12 03:54:50Z jon@bway.net $
 * Chalk
 *
 * Created 7/6/09
 * Copyright Jon Gordon
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#import "QSO.h"

static NSCalendar *gregorianCalendar = nil;

static const unsigned dateFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
static const unsigned timeFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |  NSSecondCalendarUnit;
static const unsigned allFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |  NSSecondCalendarUnit;

@implementation QSO 

@dynamic lotw_rcvd;
@dynamic wattsOut;
@dynamic qso_end;
@dynamic qsl_rcvd;
@dynamic rst_sent;
@dynamic preservedFields;
@dynamic lotw_sent;
@dynamic callsign;
@dynamic isXFreq;
@dynamic isXMode;
@dynamic mode;
@dynamic receive_mode;
@dynamic rst_rcvd;
@dynamic freq;
@dynamic receive_freq;
@dynamic qsl_sent;
@dynamic band;
@dynamic qso_date;
@dynamic comment;


+ (void)initialize
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    static BOOL initialized = NO;
    
    if (!initialized) {
        NSLog(@"  initializing");
        initialized = YES;
        gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        [gregorianCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    } // if
} // initialize


-(BOOL)validateCallsign:(id *)ioValue error:(NSError **)outError
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (*ioValue == nil) {
        return YES;
    }
    // enforce capitalization
    NSString *capitalizedName = [*ioValue uppercaseString];
    *ioValue = capitalizedName;
    return YES;
} // validateCallsign:error:


+ (NSSet *)keyPathsForValuesAffectingDatePartOfDate
{
    return [NSSet setWithObject:@"qso_date"];
} // keyPathsForValuesAffectingDatePartOfDate

- (void)setDatePartOfDate:(NSDate *)newDate
{
    NSDateComponents *comps = [gregorianCalendar components:allFlags
                                                  fromDate:self.qso_date];
    NSDateComponents *newComps = [gregorianCalendar components:dateFlags
                                                     fromDate:newDate];
    [comps setYear:[newComps year]];
    [comps setMonth:[newComps month]];
    [comps setDay:[newComps day]];
    
    self.qso_date = [gregorianCalendar dateFromComponents:comps];
} // setDatePartOfDate:

- (NSDate *)datePartOfDate
{
    return self.qso_date;
} // datePartOfDate


+ (NSSet *)keyPathsForValuesAffectingTimePartOfDate
{
    return [NSSet setWithObject:@"qso_date"];
} // keyPathsForValuesAffectingTimePartOfDate

- (void)setTimePartOfDate:(NSDate *)newTime 
{
    NSDateComponents *comps = [gregorianCalendar components:allFlags
                                                   fromDate:self.qso_date];
    NSDateComponents *newComps = [gregorianCalendar components:timeFlags
                                                      fromDate:newTime];
    [comps setHour:[newComps hour]];
    [comps setMinute:[newComps minute]];
    [comps setSecond:[newComps second]];
    
    self.qso_date = [gregorianCalendar dateFromComponents:comps];
} // setTimePartOfDate

- (NSDate *)timePartOfDate
{
    return self.qso_date;
} // timePartOfDate
@end
